package com.javafly.sms.service.security;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

import com.javafly.sms.dao.security.AuthorityDao;
import com.javafly.sms.dao.security.ResourceDao;
import com.javafly.sms.dao.security.RoleDao;
import com.javafly.sms.dao.security.UserDao;
import com.javafly.sms.entity.security.Authority;
import com.javafly.sms.entity.security.Resource;
import com.javafly.sms.entity.security.Role;
import com.javafly.sms.entity.security.User;
import com.javafly.sms.service.ServiceException;

/**
 * 安全相关实体的管理类, 包括用户,角色,资源与授权类.
 * 
 * @author calvin
 */
// Spring Service Bean的标识.
@Service
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class SecurityEntityManager {

	private static Logger logger = LoggerFactory.getLogger(SecurityEntityManager.class);

	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private AuthorityDao authorityDao;
	@Autowired
	private ResourceDao resourceDao;

	//-- User Manager --//
	@Transactional(readOnly = true)
	public User getUser(Long id) {
		return userDao.get(id);
	}

	public void saveUser(User entity) {
		userDao.save(entity);
	}
	
	/**
	 * 删除用户,如果尝试删除超级管理员将抛出异常.
	 */
	public void deleteUser(Long id) {
		if (id == 1) {
			logger.warn("操作员{}尝试删除超级管理员用户", SpringSecurityUtils
					.getCurrentUserName());
			throw new ServiceException("不能删除超级管理员用户");
		}
		userDao.delete(id);
	}
	@Transactional(readOnly = true)
	public Authority getAuth(Long id) {
		return authorityDao.get(id);
	}

	@Transactional(readOnly = true)
	public List<Authority> getAllAuth() {
		return authorityDao.getAll();
	}

	public void saveAuth(Authority entity) {
		authorityDao.save(entity);
	}

	public void deleteAuth(Long id) {
		authorityDao.delete(id);
	}
	/**
	 * 使用属性过滤条件查询用户.
	 */
	@Transactional(readOnly = true)
	public Page<User> searchUser(final Page<User> page, final List<PropertyFilter> filters) {
		return userDao.findPage(page, filters);
	}

	@Transactional(readOnly = true)
	public User findUserByLoginName(String loginName) {
		return userDao.findUniqueBy("loginName", loginName);
	}

	/**
	 * 检查用户名是否唯一.
	 * 
	 * @return loginName在数据库中唯一或等于oldLoginName时返回true.
	 */
	@Transactional(readOnly = true)
	public boolean isLoginNameUnique(String newLoginName, String oldLoginName) {
		return userDao.isPropertyUnique("loginName", newLoginName, oldLoginName);
	}

	//-- Role Manager --//
	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	@Transactional(readOnly = true)
	public List<Role> getAllRole() {
		return roleDao.getAll();
	}

	public void saveRole(Role entity) {
		roleDao.save(entity);
	}

	public void deleteRole(Long id) {
		roleDao.delete(id);
	}

	//-- Resource Manager --//
	/**
	 * 查找URL类型的资源并初始化可访问该资源的授权.
	 */
	@Transactional(readOnly = true)
	public List<Resource> getUrlResourceWithAuthorities() {
		return resourceDao.getUrlResourceWithAuthorities();
	}
	@Transactional(readOnly = true)
	public List<Resource> getMenuResource() {
		return resourceDao.getMenuResource();
	}
	//-- Authority Manager --//
	@Transactional(readOnly = true)
	public List<Authority> getAllAuthority() {
		return authorityDao.getAll();
	}
	@Transactional(readOnly = true)
	public List<Resource> getDoResource() {
		// TODO Auto-generated method stub
		return resourceDao.getDoResource();
	}
	@Transactional(readOnly = true)
	public Resource getResource(Long id) {
		// TODO Auto-generated method stub
		return resourceDao.get(id);
	}
	@Transactional(readOnly = true)
	public List<Resource> getSubResource(Long id) {
		// TODO Auto-generated method stub
		return resourceDao.getSubResourceWithAuthorities(id);
		
		
	}
	/**
	 * 刷新 用户权限
	 */
	public void resetRight() throws Exception {
		//SpringContextUtil ee;
		SpringSecurityUtils.refreshDefinitionSource(org.springside.modules.spring.SpringContextHolder.getApplicationContext(),"filterSecurityInterceptor", "definitionSource");
	}
	@Transactional(readOnly = true)
	public List<Resource> getAllResource() {
		// TODO Auto-generated method stub
		return resourceDao.getAll();
	}

	public void saveResource(Resource entity) {
		// TODO Auto-generated method stub
		resourceDao.save(entity);
	}
	public void deleteResource(Long id) {
		resourceDao.delete(id);
	}
	@Transactional(readOnly = true)
	public Page<Resource> searchResource(Page<Resource> page,
			List<PropertyFilter> filters) {
		return resourceDao.findPage(page, filters);
	}

	public Page<Authority> searchAuth(Page<Authority> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return authorityDao.findPage(page, filters);
	}
}
